package com.yisu.auction.manager.core.service.impl;


import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import com.yisu.auction.ewf.common.web.HttpWebClient;
import com.yisu.auction.ewf.common.web.JsonResult;
import com.yisu.auction.ewf.entity.GoodsInfo;
import com.yisu.auction.ewf.entity.Prize;
import com.yisu.auction.ewf.entity.SystemConfig;
import com.yisu.auction.manager.core.service.IPrizeService;
import com.yisu.auction.manager.core.mapper.GoodsInfoMapper;
import com.yisu.auction.manager.core.mapper.PrizeMapper;
import com.yisu.auction.manager.core.service.impl.func.LuckyNumberUtil;
import com.yisu.auction.manager.domain.request.baseInfoPackage.GoodsInfoRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

/**
 * author: 赵杰
 * Business Description：奖品类相关业务实现
 * createTime：2017/8/10
 */
@Service("IPrizeService")
public class PrizeServiceImpl extends BaseService implements IPrizeService {
    @Resource
    private PrizeMapper prizeMapper;
    @Resource
    private GoodsInfoMapper goodsInfoMapper;
    @Resource
    private com.yisu.auction.manager.domain.config.SystemConfig systemConfig;

    /**
     * author:赵杰
     * Business Description : 通过商品ID，开始进行夺宝操作
     *
     * @param goodsId 商品ID
     * @return
     */
    @Override
    public boolean startSnatchTreasure(long goodsId) {
        GoodsInfo goodsInfo = goodsInfoMapper.selectByPrimaryKey(goodsId);
        if (goodsInfo == null || (goodsInfo != null && goodsInfo.getStatus() == GoodsInfo.StatusEnum.SNATCH_TREASURE_ING.code))
            return false;
        //获取最大期号
        Integer maxTerm = goodsInfoMapper.getMaxTermByGoodsId(goodsId) + 1;
        //生成幸运号，成功则开启新一期夺宝
        GoodsInfo updateGoodsInfo = new GoodsInfo();
        updateGoodsInfo.setId(goodsId);
        updateGoodsInfo.setTerm(maxTerm);
        updateGoodsInfo.setStatus(GoodsInfo.StatusEnum.SNATCH_TREASURE_ING.code);
        goodsInfoMapper.updateByPrimaryKeySelective(updateGoodsInfo);
        Prize insertPrize = new Prize();
        insertPrize.setCreateDate(new Date());
        insertPrize.setIsValid(true);
        insertPrize.setGoodsId(goodsId);
        insertPrize.setStartTime(new Date());
        insertPrize.setPrice(goodsInfo.getPrice());
        insertPrize.setName(goodsInfo.getName());
        insertPrize.setGoodsTypeId(goodsInfo.getGoodsTypeId());
        insertPrize.setTerm(maxTerm);
        insertPrize.setImage(goodsInfo.getImages());
        insertPrize.setStatus((byte) Prize.StatusTypeEnum.SNATCH_TREASURE_ING.code);
        insertPrize.setIsTen(goodsInfo.getIsTen());
        insertPrize.setIsRechargeableCard(goodsInfo.getIsRechargeableCard());
        insertPrize.setGoodsDetail(goodsInfo.getDetail());
        prizeMapper.insertSelective(insertPrize);
        //获取当前期号的prizeId
        GoodsInfoRequest goodsInfoRequest = new GoodsInfoRequest();
        goodsInfoRequest.setId(insertPrize.getGoodsId());
        goodsInfoRequest.setTerm(maxTerm);
        Long prizeId = prizeMapper.getPrizeIdByGoodsIdAndTerm(goodsInfoRequest);
        if (prizeId != null && prizeId > 0) {
            Map<String, String> maps = Maps.newConcurrentMap();
            maps.put("prizeId", prizeId + "");
            String resultJson = HttpWebClient.post(systemConfig.getAuctionJobUrl()+"/job/addJob", maps);
      /*      String resultJson = HttpWebClient.post("http://192.168.206.225:8080/job/addJob", maps);*/
            JsonResult jsonResult = JSON.parseObject(resultJson, JsonResult.class);
            if (jsonResult != null) {
               if(jsonResult.getErrorCode()!=0)
                   throw new RuntimeException("运行异常");
            }
        }
        return true;
    }
}
